package zhongbo.java.air.monitor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.monitor.mapper.FlightActivityDailyMapper;
import zhongbo.java.air.monitor.model.AirAlertLog;
import zhongbo.java.air.monitor.model.FlightActivityDaily;
import zhongbo.java.air.monitor.service.IFlightActivityDailyService;

import java.util.Date;
import java.util.List;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月08日 星期二 04:07:41
 */
@Service
@Slf4j
public class FlightActivityDailyServiceImpl implements IFlightActivityDailyService<FlightActivityDaily> {

    @Autowired
    private FlightActivityDailyMapper flightActivityDailyMapper;

    @Override
    public List<FlightActivityDaily> selectDailyStats(String date) {
        return flightActivityDailyMapper.selectDailyStats(date);
    }

    @Override
    public List<FlightActivityDaily> getDailyStats(String date) {
        if (StrUtil.isBlank(date)) {
            throw new RuntimeException("date参数必须填写!示例: 2025-04-08");
        }
        // 根据 date 条件, 从 air_flight_activity_daily 表里查询结果,并返回
        return flightActivityDailyMapper.getDailyStats(date);
    }

    @Override
    public List<FlightActivityDaily> selectByIcaoAndDate(String icaoCode, Date date) {
        if (StrUtil.isBlank(icaoCode) || date == null) {
            throw new RuntimeException("icaoCode和date参数必须填写!示例: 2025-04-08");
        }
        return flightActivityDailyMapper.selectByIcaoAndDate(icaoCode, date);
    }

//    @Override
//    public FlightActivityDaily insert(FlightActivityDaily flightActivityDaily) {
//        if (flightActivityDaily == null){
//            throw new RuntimeException("新增失败");
//        }
//        int insert = flightActivityDailyMapper.insert(flightActivityDaily);
//        if (insert > 0) {
//            return flightActivityDaily;
//        }
//        return null;
//    }
@Override
public FlightActivityDaily add(FlightActivityDaily entity) {
    if (entity == null){
        throw new RuntimeException("新增失败");
    }
    int insert = flightActivityDailyMapper.insert(entity);
    if (insert > 0) {
        return entity;
    }
    return null;
}

//    @Override
//    public FlightActivityDaily update(FlightActivityDaily flightActivityDaily) {
//        if (flightActivityDaily == null){
//            throw new RuntimeException("更新失败");
//        }
//        int update = flightActivityDailyMapper.updateById(flightActivityDaily);
//        if (update > 0) {
//            return flightActivityDaily;
//        }
//        return null;
//    }
@Override
public FlightActivityDaily edit(FlightActivityDaily edit) {
    if (edit == null){
        throw new RuntimeException("更新失败");
    }
    int update = flightActivityDailyMapper.updateById(edit);
    if (update > 0) {
        return edit;
    }
    return null;
}

//    @Override
//    public boolean deleteById(String id) {
//        if (id == null){
//            throw new RuntimeException("id不能为空");
//        }
//        log.info("删除ID为 {} 的飞行活动记录", id);
//        int delete = flightActivityDailyMapper.deleteById(id);
//        if (delete == 0){
//            throw new RuntimeException("删除失败");
//        }
//        return delete > 0;
//    }
    @Override
    public boolean delete(String entityId) {
        if (entityId == null){
            throw new RuntimeException("id不能为空");
        }
        log.info("删除ID为 {} 的飞行活动记录", entityId);
        int delete = flightActivityDailyMapper.deleteById(entityId);
        if (delete == 0){
            throw new RuntimeException("删除失败");
        }
        return delete > 0;
    }

//    @Override
//    public FlightActivityDaily queryById(String id) {
//        if (id == null){
//            throw new RuntimeException("id不能为空");
//        }
//        log.info("查询ID为 {} 的飞行活动记录", id);
//        if (flightActivityDailyMapper.selectById(id) == null){
//            throw new RuntimeException("id不存在");
//        }
//        return flightActivityDailyMapper.selectById(id);
//    }
@Override
public FlightActivityDaily findOne(String entityId) {
    if (entityId == null){
        throw new RuntimeException("id不能为空");
    }
    log.info("查询ID为 {} 的飞行活动记录", entityId);
    if (flightActivityDailyMapper.selectById(entityId) == null){
        throw new RuntimeException("id不存在");
    }
    return flightActivityDailyMapper.selectById(entityId);
}

    @Override
    public PageVo<FlightActivityDaily> findPage(int page, int pageSize, String keyword, String fields, String oderByField, boolean orderByAsc) {
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field),keyword);
            }
        }
        IPage<FlightActivityDaily> pageParam = new Page<>(page, pageSize);
        IPage airAlertZoneIPage = flightActivityDailyMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(airAlertZoneIPage);
    }

}